export default {
  data() {
    return {
      params: {
      	pageNum: 1,
      	pageSize: 10,
      },
      listData: [],
      isMore: true,
      loading: false,
      listFormat: null,
    }
  },
  computed: {
    listStatus() {
      if (this.loading) return 'loading';
      else if (this.listData.length === 0) return 'empty';
      else if (this.isMore) return 'more';
      else if (!this.isMore) return 'noMore';
    }
  },
  methods: {
    /** @name 获取列表数据 **/
    list() {
      this.params.pageNum++;
      this.loading = true;
      this.API(this.params).then(res => {
        let data = res.rows;
        if (data.length > 0) {
          this.listData = this.listData.concat(data);
          if (data.length < this.params.pageSize || this.params.pageNum >= res.total/this.params.pageSize) {
            this.isMore = false;
          } else {
            this.isMore = true;
          }
          this.listFormat && this.listFormat(this.listData)
        } else {
          this.isMore = false;
        }
      }).finally(() => {
        this.loading = false;
      })
    },
    /** @name 获取列表数据-刷新 **/
    listRefresh() {
      this.params.pageNum = 1;
      this.loading = true;
      this.listData = [];
      this.API(this.params).then(res => {
        let data = res.rows;
        if (data.length > 0) {
          this.listData = data;
          if (data.length < this.params.pageSize || this.params.pageNum >= res.total/this.params.pageSize) {
            this.isMore = false;
          } else {
            this.isMore = true;
          }
          this.listFormat && this.listFormat(this.listData)
        } else {
          this.listData = [];
          this.isMore = false;
        }
      }).finally(() => {
        this.loading = false;
      })
    },
  }
}

/** @name 上拉加载 **/
export function onReachBottom() {
  this.isMore && this.list();
}

/** @name 下拉刷新 **/
export function onPullDownRefresh(_this) {
  _this ? _this.listRefresh() : this.listRefresh();
  setTimeout(() => {
    uni.stopPullDownRefresh()
  }, 1000)
}
